
var config = {
    timmer:{
        baseUrl:"http://localhost:8080/timmer",
        jsessionid:"",
        listAll:"/job/listAll",
        listJobState:"/job/listJobState",
        addJob:"/job/addJob",
        addAndScheduleJob:"/job/addAndScheduleJob",
        pauseJob:"/job/pauseJob",
        removeJob:"/job/removeJob",
        scheduleJob:"/job/scheduleJob"
    }
};

angular.module('myApp', ['ui.table']);
angular.module('myApp').controller('TaskCtrl', function ($scope) {
    $scope.tasks = [];
    $scope.dbTasks = [];
    $scope.editedTask = {};
    $scope.triggerName = "";
    $scope.triggerGroupName = "";
    $scope.cronExpression = "";
    $scope.init = function () {
      http.get("./system/menu/getTimmerBaseUrl",function (data) {
          config.timmer.baseUrl = data.baseUrl;
          config.timmer.jsessionid = data.JSESSIONID;
          $scope._init();
      })
    };
    $scope._init = function(){
        http.get(config.timmer.baseUrl + config.timmer.listAll + "?backendSessionId=" + config.timmer.jsessionid, function(data){
            for(var key in data){
                $scope.$apply(function() {
                    $scope.tasks.push(data[key]);
                });
            }
        });
        http.get(config.timmer.baseUrl+config.timmer.listJobState + "?backendSessionId=" + config.timmer.jsessionid, function(data){
            for(var key in data){
                $scope.$apply(function() {
                    $scope.dbTasks.push(data[key]);
                });
            }
        });
    };
    $scope.addJob = function(task){
        http.post(config.timmer.baseUrl + config.timmer.addJob + "?backendSessionId=" + config.timmer.jsessionid,task, function(data){
            $('#myModal').modal('hide')
            $scope.$apply(function() {
                $scope.message = "任务添加成功";
                $scope.dbTasks.push(task);
            });
            $('#messageModel').modal('show');
        });
    };
    $scope.addAndScheduleJob = function(task){
        http.post(config.timmer.baseUrl + config.timmer.addAndScheduleJob + "?backendSessionId=" + config.timmer.jsessionid, task, function(data){
            $('#myModal').modal('hide')
            $scope.$apply(function() {
                $scope.message = "任务调度成功";
                $scope.dbTasks.push(task);
                $('.collapse').collapse('hide');
            });
            $('#messageModel').modal('show');
        });
    };

    $scope.startJob = function(task){
        if(!task.cronExpression){
            $scope.editedTask.jobName = task.jobName;
            $scope.editedTask.jobGroupName = task.jobGroupName;
            $("#editCronModel").modal("show");
            return;
        }else{
            $scope._submitTrigger(task);
        }
    };
    $scope.scheduleJob = function(){
        $scope._submitTrigger($scope.editedTask);
    };
    $scope.pauseJob = function(task){
        http.post(config.timmer.baseUrl+config.timmer.pauseJob + "?backendSessionId=" + config.timmer.jsessionid, task, function(data){
            $scope.$apply(function() {
                $scope.message = "任务启动已暂停";
                 for(var key in $scope.dbTasks){
                    if($scope.dbTasks[key].jobName == task.jobName){
                        $scope.dbTasks[key].triggerState = 'PAUSED';
                        break;
                    }
                }
             });
            $('#messageModel').modal('show');
        },function(data){
            $scope.$apply(function() {
                $scope.message = data.code + data.msg;
            });
            $('#messageModel').modal('show');
        });
    };
    $scope.removeJob = function(task){
        http.post(config.timmer.baseUrl + config.timmer.removeJob + "?backendSessionId=" + config.timmer.jsessionid, task, function(data){
            $scope.$apply(function() {
                $scope.message = "任务启动已移除";
                var copy = [];
                for(var key in $scope.dbTasks){
                    if($scope.dbTasks[key].jobName != task.jobName){
                        copy.push($scope.dbTasks[key]);
                    }
                }
                $scope.dbTasks = copy;
            });
            $('#messageModel').modal('show');
        },function(data){
            $scope.$apply(function() {
                $scope.message = data.code + data.msg;
            });
            $('#messageModel').modal('show');
        });
    };
    $scope._submitTrigger = function (params) {
        http.post(config.timmer.baseUrl + config.timmer.scheduleJob + "?backendSessionId=" + config.timmer.jsessionid, params, function(data){
            $('#editCronModel').modal('hide');
            $scope.$apply(function() {
                $scope.message = "任务启动成功";
                for(var key in $scope.dbTasks){
                    if($scope.dbTasks[key].jobName == params.jobName){
                        $scope.dbTasks[key].triggerName = params.triggerName;
                        $scope.dbTasks[key].triggerGroupName = params.triggerGroupName;
                        $scope.dbTasks[key].cronExpression = params.cronExpression;
                        $scope.dbTasks[key].triggerState = "NORMAL";
                    }
                }
                params.triggerState = "NORMAL";
            });
            $('#messageModel').modal('show');
        },function(data){
            $('#editCronModel').modal('hide');
            $scope.$apply(function() {
                $scope.message = data.code + data.msg;
            });
            $('#messageModel').modal('show');
        });
    };
    $scope.showStart = function(index){
        return $scope.dbTasks[index].triggerState == 'PAUSED';
    };
    $scope.showPause = function(index){
        return $scope.dbTasks[index].triggerState == 'NORMAL';
    };
    $scope.showConfig = function(index){
        return $scope.dbTasks[index].cronExpression?false:true;
    };
});
$('.modal').on('hidden.bs.modal', function (e) {
    $(this).addClass("hide");
}).on("show.bs.modal",function(e){
    $(this).removeClass("hide");
});
